/*     */package imageViewer;

/*     */
/*     */import java.io.ByteArrayInputStream;
/*     */
import java.io.ByteArrayOutputStream;
/*     */
import java.io.IOException;
/*     */
import java.io.InputStream;
/*     */
import java.net.HttpURLConnection;
/*     */
import java.net.URL;
/*     */
import java.util.List;
/*     */
import java.util.concurrent.Semaphore;
/*     */
import javafx.application.Platform;
/*     */
import javafx.beans.property.DoubleProperty;
/*     */
import javafx.beans.property.ReadOnlyDoubleProperty;
/*     */
import javafx.beans.value.ChangeListener;
/*     */
import javafx.beans.value.ObservableValue;
/*     */
import javafx.collections.ObservableList;
/*     */
import javafx.event.Event;
/*     */
import javafx.event.EventHandler;
/*     */
import javafx.geometry.Insets;
/*     */
import javafx.geometry.Pos;
/*     */
import javafx.scene.Node;
/*     */
import javafx.scene.Scene;
/*     */
import javafx.scene.canvas.Canvas;
/*     */
import javafx.scene.canvas.GraphicsContext;
/*     */
import javafx.scene.control.Label;
/*     */
import javafx.scene.image.Image;
/*     */
import javafx.scene.image.ImageView;
/*     */
import javafx.scene.input.KeyCode;
/*     */
import javafx.scene.input.KeyEvent;
/*     */
import javafx.scene.input.MouseEvent;
/*     */
import javafx.scene.layout.AnchorPane;
/*     */
import javafx.scene.layout.HBox;
/*     */
import javafx.scene.layout.Priority;
/*     */
import javafx.scene.layout.StackPane;
/*     */
import javafx.scene.layout.VBox;
/*     */
import javafx.scene.paint.Color;
/*     */
import javafx.stage.Stage;
/*     */
import javafx.stage.WindowEvent;
/*     */
import javafx.util.Callback;

/*     */
/*     */public class Gallery extends VBox
/*     */{
	/*     */private final Stage parentStage;
	/*     */private int canvasWidth;
	/*     */private double factor;
	/*     */private double imageGalleryWidth;
	/*     */private double imageGalleryHeight;
	/*     */private Image imageGallery;
	/* 63 */private ChangeListener resizeListener = null;
	/*     */final AnchorPane mainWrapper;
	/* 69 */private int actualImageIndex = 0;
	/*     */
	/* 72 */private double percentageActivation = 0.5D;
	/*     */private final Canvas canvas;
	/*     */private final List<PhotoGallery> photos;
	/*     */private final Label left;
	/*     */private final Label right;
	/*     */private final Label close;
	/*     */private final Label loading;
	/*     */private Label counterLabel;
	/*     */private ThumbnailsManager thumbnailsManager;
	/* 93 */final Semaphore semaphore = new Semaphore(1);
	/*     */private final EventHandler<KeyEvent> galleryControls;

	/*     */
	/*     */public Gallery(final Stage parentStage,
			final List<PhotoGallery> photos, Double percentagePrevImage)
	/*     */{
		/* 113 */getStyleClass().add("gallery-modal");
		/*     */
		/* 115 */if ((photos == null) || (photos.isEmpty())) {
			/* 116 */throw new IllegalArgumentException("Empty or null urls");
			/*     */}
		/*     */
		/* 119 */this.photos = photos;
		/*     */
		/* 121 */if (percentagePrevImage != null) {
			/* 122 */this.percentageActivation = percentagePrevImage
					.doubleValue();
			/*     */}
		/*     */
		/* 125 */this.parentStage = parentStage;
		/*     */
		/* 127 */this.resizeListener = new ChangeListener()
		/*     */{
			/*     */public void changed(ObservableValue arg0, Object arg1,
					Object arg2)
			/*     */{
				/* 132 */Gallery.this.syncParentStage(parentStage);
				/* 133 */Gallery.this.redraw();
				/*     */}
			/*     */
		};
		/* 138 */parentStage.heightProperty().addListener(this.resizeListener);
		/* 139 */parentStage.widthProperty().addListener(this.resizeListener);
		/*     */
		/* 141 */parentStage.setOnCloseRequest(new EventHandler()
		/*     */{
			/*     */public void handle(WindowEvent arg0) {
				/* 144 */((StackPane) parentStage.getScene().getRoot())
						.getChildren().remove(Gallery.this);
				/*     */}
			/*     */
		});
		/* 161 */this.mainWrapper = new AnchorPane();
		/*     */
		/* 164 */HBox main = new HBox();
		/* 165 */AnchorPane.setBottomAnchor(main, Double.valueOf(0.0D));
		/* 166 */AnchorPane.setTopAnchor(main, Double.valueOf(0.0D));
		/* 167 */AnchorPane.setLeftAnchor(main, Double.valueOf(0.0D));
		/* 168 */AnchorPane.setRightAnchor(main, Double.valueOf(0.0D));
		/*     */
		/* 170 */main.setAlignment(Pos.CENTER);
		/*     */
		/* 172 */this.canvas = new Canvas();
		/*     */
		/* 174 */this.left = new Label();
		/* 175 */this.left.setVisible(false);
		/* 176 */this.left.getStyleClass().add("left");
		/*     */
		/* 178 */this.right = new Label();
		/* 179 */this.right.setVisible(false);
		/* 180 */this.right.getStyleClass().add("right");
		/*     */
		/* 182 */this.close = new Label();
		/* 183 */this.close.getStyleClass().add("close");
		/* 184 */this.close.setPrefSize(19.0D, 19.0D);
		/* 185 */this.close.setVisible(false);
		/*     */
		/* 187 */final Canvas tooltip = new Canvas();
		/* 188 */tooltip.setWidth(100.0D);
		/* 189 */tooltip.setHeight(40.0D);
		/* 190 */tooltip.setVisible(false);
		/* 191 */GraphicsContext gc = tooltip.getGraphicsContext2D();
		/* 192 */gc.setFill(Color.BLACK);
		/* 193 */gc.fillRect(5.0D, 5.0D, 60.0D, 22.0D);
		/* 194 */int inicio = 46;
		/* 195 */gc.fillPolygon(new double[] { 46.0D, 51.0D, 56.0D },
				new double[] { 5.0D, 0.0D, 5.0D }, 3);
		/*     */
		/* 197 */gc.setFill(Color.WHITE);
		/* 198 */gc.fillText("Cerrar", 15.0D, 21.0D);
		/*     */
		/* 200 */this.close.setOnMouseEntered(new EventHandler()
		/*     */{
			/*     */public void handle(MouseEvent arg0) {
				/* 203 */tooltip.setVisible(true);
				/*     */}
			/*     */
		});
		/* 206 */this.close.setOnMouseExited(new EventHandler()
		/*     */{
			/*     */public void handle(MouseEvent arg0) {
				/* 209 */tooltip.setVisible(false);
				/*     */}
			/*     */
		});
		/* 212 */this.close.setOnMouseClicked(new EventHandler()
		/*     */{
			/*     */public void handle(MouseEvent arg0) {
				/* 215 */Gallery.this.close();
				/*     */}
			/*     */
		});
		/* 219 */this.loading = new Label();
		/* 220 */this.loading.setPrefSize(30.0D, 30.0D);
		/* 221 */this.loading.getStyleClass().add("loading");
		/* 222 */this.loading.setAlignment(Pos.CENTER);
		/* 223 */this.loading.setGraphic(new ImageView(new Image(Gallery.class
				.getResourceAsStream("/loading.gif"))));
		/*     */
		/* 225 */this.loading.setVisible(false);
		/*     */
		/* 227 */this.close.layoutXProperty().addListener(new ChangeListener()
		/*     */{
			/*     */public void changed(ObservableValue<? extends Number> arg0,
					Number oldValue, Number newValue)
			/*     */{
				/* 232 */tooltip.setLayoutX(newValue.doubleValue() - 42.0D);
				/*     */}
			/*     */
		});
		/* 235 */this.close.layoutYProperty().addListener(new ChangeListener()
		/*     */{
			/*     */public void changed(ObservableValue<? extends Number> arg0,
					Number oldValue, Number newValue)
			/*     */{
				/* 240 */tooltip.setLayoutY(newValue.doubleValue() + 22.0D);
				/*     */}
			/*     */
		});
		/* 244 */this.canvas.layoutXProperty().addListener(new ChangeListener()
		/*     */{
			/*     */public void changed(ObservableValue<? extends Number> arg0,
					Number oldValue, Number newValue)
			/*     */{
				/* 249 */Gallery.this.left.setLayoutX(newValue.doubleValue());
				/* 250 */Gallery.this.right.setLayoutX(newValue.doubleValue()
						+ Gallery.this.canvas.getWidth() / 2.0D);
				/*     */
				/* 252 */Gallery.this.loading.setLayoutX(newValue.doubleValue()
						+ Gallery.this.canvas.getWidth() / 2.0D - 15.0D);
				/*     */
				/* 254 */Gallery.this.close.setLayoutX(newValue.doubleValue()
						+ Gallery.this.canvas.getWidth() - 22.0D - 10.0D);
				/*     */}
			/*     */
		});
		/* 258 */this.canvas.layoutYProperty().addListener(new ChangeListener()
		/*     */{
			/*     */public void changed(ObservableValue<? extends Number> arg0,
					Number oldValue, Number newValue)
			/*     */{
				/* 263 */Gallery.this.left.setLayoutY(newValue.doubleValue());
				/* 264 */Gallery.this.right.setLayoutY(newValue.doubleValue());
				/* 265 */Gallery.this.loading.setLayoutY(newValue.doubleValue()
						+ Gallery.this.canvas.getHeight() / 2.0D - 15.0D);
				/*     */
				/* 267 */Gallery.this.close.setLayoutY(newValue.doubleValue() + 10.0D);
				/*     */}
			/*     */
		});
		/* 270 */this.canvas.widthProperty().addListener(new ChangeListener()
		/*     */{
			/*     */public void changed(ObservableValue<? extends Number> arg0,
					Number oldValue, Number newValue)
			/*     */{
				/* 275 */Gallery.this.left.setPrefWidth(newValue.doubleValue() / 2.0D);
				/* 276 */Gallery.this.right.setPrefWidth(newValue.doubleValue() / 2.0D);
				/*     */}
			/*     */
		});
		/* 279 */this.canvas.heightProperty().addListener(new ChangeListener()
		/*     */{
			/*     */public void changed(ObservableValue<? extends Number> arg0,
					Number oldValue, Number newValue)
			/*     */{
				/* 284 */Gallery.this.left
						.setPrefHeight(Gallery.this.imageGalleryHeight
								* Gallery.this.factor);
				/* 285 */Gallery.this.right
						.setPrefHeight(Gallery.this.imageGalleryHeight
								* Gallery.this.factor);
				/*     */}
			/*     */
		});
		/* 289 */main.getChildren().addAll(new Node[] { this.canvas });
		/* 290 */VBox.setVgrow(this.mainWrapper, Priority.ALWAYS);
		/*     */
		/* 292 */this.mainWrapper.getChildren().addAll(
				new Node[] { main, this.left, this.right, this.loading,
						this.close, tooltip });
		/*     */
		/* 295 */HBox counterPane = new HBox();
		/* 296 */counterPane.setAlignment(Pos.CENTER_RIGHT);
		/* 297 */VBox.setMargin(counterPane,
				new Insets(0.0D, 20.0D, 0.0D, 0.0D));
		/*     */
		/* 299 */this.counterLabel = new Label();
		/* 300 */this.counterLabel.getStyleClass().add("counter");
		/* 301 */counterPane.getChildren().add(this.counterLabel);
		/*     */
		/* 303 */this.thumbnailsManager = new ThumbnailsManager(photos,
				new Callback()
				/*     */{
					/*     */public Void call(PhotoGallery photo)
					/*     */{
						/* 308 */Gallery.this.actualImageIndex = photos
								.indexOf(photo);
						/*     */
						/* 310 */Gallery.this.updateImage(photo);
						/*     */
						/* 312 */return null;
						/*     */}
					/*     */
				});
		/* 315 */VBox.setMargin(this.thumbnailsManager, new Insets(0.0D));
		/*     */
		/* 317 */getChildren().addAll(
				new Node[] { this.mainWrapper, counterPane,
						this.thumbnailsManager });
		/*     */
		/* 319 */parentStage.getScene().getStylesheets()
				.add(getClass().getResource("/gallery.css").toExternalForm());
		/*     */
		/* 321 */((StackPane) parentStage.getScene().getRoot()).getChildren()
				.add(this);
		/*     */
		/* 323 */EventHandler mouseEventHandler = new EventHandler()
		/*     */{
			/*     */public void handle(MouseEvent event) {
				/* 326 */if ((!Gallery.this.left.contains(event.getX(),
						event.getY()))
						&& (!Gallery.this.right.contains(event.getX(),
								event.getY())))
				/*     */{
					/* 328 */Gallery.this.left.setVisible(false);
					/* 329 */Gallery.this.right.setVisible(false);
					/*     */}
				/*     */}
			/*     */
		};
		/* 334 */this.left.setOnMouseExited(mouseEventHandler);
		/* 335 */this.right.setOnMouseExited(mouseEventHandler);
		/*     */
		/* 337 */this.canvas.setOnMouseEntered(new EventHandler()
		/*     */{
			/*     */public void handle(MouseEvent mouseEvent) {
				/* 340 */Gallery.this.left.setVisible(true);
				/* 341 */Gallery.this.right.setVisible(true);
				/*     */}
			/*     */
		});
		/* 345 */this.left.setOnMouseClicked(new EventHandler()
		/*     */{
			/*     */public void handle(Event arg0) {
				/* 348 */Gallery.this.prevImage();
				/*     */}
			/*     */
		});
		/* 352 */this.right.setOnMouseClicked(new EventHandler()
		/*     */{
			/*     */public void handle(Event arg0) {
				/* 355 */Gallery.this.nextImage();
				/*     */}
			/*     */
		});
		/* 359 */this.galleryControls = new EventHandler()
		/*     */{
			/*     */public void handle(KeyEvent keyEvent)
			/*     */{
				/* 363 */if (keyEvent.getCode() == KeyCode.LEFT)
					/* 364 */Gallery.this.prevImage();
				/* 365 */else if (keyEvent.getCode() == KeyCode.RIGHT)
					/* 366 */Gallery.this.nextImage();
				/* 367 */else if (keyEvent.getCode() == KeyCode.ESCAPE)
					/* 368 */Gallery.this.close();
				/*     */}
			/*     */
		};
		/* 375 */parentStage.getScene().addEventHandler(KeyEvent.KEY_RELEASED,
				this.galleryControls);
		/*     */}

	/*     */
	/*     */public void show()
	/*     */{
		/* 384 */show(0);
		/*     */}

	/*     */
	/*     */public void show(int index)
	/*     */{
		/* 395 */syncParentStage(this.parentStage);
		/*     */
		/* 397 */this.actualImageIndex = index;
		/*     */
		/* 399 */this.counterLabel
				.setText(index + 1 + "/" + this.photos.size());
		/*     */
		/* 401 */updateImage((PhotoGallery) this.photos
				.get(this.actualImageIndex));
		/*     */}

	/*     */
	/*     */public void close()
	/*     */{
		/* 410 */((StackPane) this.parentStage.getScene().getRoot())
				.getChildren().remove(this);
		/*     */
		/* 412 */this.parentStage.getScene().removeEventHandler(
				KeyEvent.KEY_RELEASED, this.galleryControls);
		/*     */}

	/*     */
	/*     */private final void nextImage()
	/*     */{
		/* 421 */updateImage((PhotoGallery) this.photos.get(getNextImage()));
		/*     */}

	/*     */
	/*     */private final void prevImage()
	/*     */{
		/* 428 */updateImage((PhotoGallery) this.photos.get(getPrevImage()));
		/*     */}

	/*     */
	/*     */private final void updateImage(final PhotoGallery photo)
/*     */   {
/* 439 */     final int position = this.photos.indexOf(photo) + 1;
/*     */
/* 441 */     this.counterLabel.setText(position + "/" + this.photos.size());
/*     */
/* 443 */     new Thread()
/*     */     {
/*     */       public void run()
/*     */       {
/*     */         try
/*     */         {
/* 449 */           Gallery.this.semaphore.acquire();
/*     */         } catch (InterruptedException e) {
/*     */         }
/* 452 */         Platform.runLater(new Runnable()
/*     */         {
/*     */           public void run()
/*     */           {
/* 456 */             Gallery.this.refreshImage2(Gallery.18.this.val$photo, Gallery.18.this.val$position);
/*     */           }
/*     */         });
/*     */       }
/*     */     }
/* 443 */     .start();
/*     */   }

	/*     */
	/*     */private final void refreshImage2(final PhotoGallery photo, final int position)
/*     */   {
/* 477 */     this.loading.setVisible(true);
/*     */
/* 479 */     new Thread()
/*     */     {
/*     */       public void run()
/*     */       {
/*     */         try
/*     */         {
/* 488 */           URL url = new URL(photo.getPhotoUrl());
/* 489 */           HttpURLConnection connection = (HttpURLConnection)url.openConnection();
/*     */
/* 491 */           InputStream inputStream = connection.getInputStream();
/* 492 */           final byte[] image = Gallery.toByteArray(inputStream);
/* 493 */           inputStream.close();
/* 494 */           connection.disconnect();
/*     */
/* 496 */           Platform.runLater(new Runnable()
/*     */           {
/*     */             public void run()
/*     */             {
/* 501 */               Gallery.this.thumbnailsManager.updateThumbnail(Gallery.19.this.val$photo.getThumbnailUrl(), Gallery.19.this.val$position);
/*     */
/* 504 */               Gallery.this.imageGallery = new Image(new ByteArrayInputStream(image));
/*     */
/* 506 */               Gallery.this.imageGalleryWidth = Gallery.this.imageGallery.getWidth();
/* 507 */               Gallery.this.imageGalleryHeight = Gallery.this.imageGallery.getHeight();
/*     */
/* 509 */               Gallery.this.doCalculations();
/*     */
/* 511 */               Gallery.this.loading.setVisible(false);
/*     */
/* 513 */               Gallery.this.redraw();
/*     */
/* 515 */               Gallery.this.semaphore.release();
/*     */             } } );
/*     */         } catch (Exception e1) {
/* 518 */           e1 =
/* 520 */             e1;
/*     */         }
/*     */         finally
/*     */         {
/*     */         }
/*     */       }
/*     */     }
/* 479 */     .start();
/*     */   }

	/*     */
	/*     */private void redraw()
	/*     */{
		/* 531 */double height = this.imageGalleryHeight * this.factor;
		/*     */
		/* 533 */if (this.imageGallery != null)
		/*     */{
			/* 535 */this.canvas.getGraphicsContext2D().drawImage(
					this.imageGallery, 0.0D, 0.0D, this.imageGalleryWidth,
					this.imageGalleryHeight, 0.0D, 0.0D, this.canvasWidth,
					height);
			/*     */}
		/*     */
		/* 541 */this.canvas.getGraphicsContext2D().setStroke(
				new Color(1.0D, 1.0D, 1.0D, 0.3D));
		/* 542 */this.canvas.getGraphicsContext2D().setLineWidth(1.0D);
		/* 543 */this.canvas.getGraphicsContext2D().strokeRect(1.0D, 1.0D,
				this.canvasWidth - 2, height - 2.0D);
		/*     */
		/* 547 */this.close.setVisible(true);
		/*     */}

	/*     */
	/*     */private int getPrevImage()
	/*     */{
		/* 558 */if (--this.actualImageIndex < 0) {
			/* 559 */this.actualImageIndex = (this.photos.size() - 1);
			/*     */}
		/* 561 */return this.actualImageIndex;
		/*     */}

	/*     */
	/*     */private int getNextImage()
	/*     */{
		/* 572 */if (++this.actualImageIndex >= this.photos.size()) {
			/* 573 */this.actualImageIndex = 0;
			/*     */}
		/* 575 */return this.actualImageIndex;
		/*     */}

	/*     */
	/*     */private final void doCalculations()
	/*     */{
		/* 584 */double parentHeight = this.mainWrapper.getHeight();
		/* 585 */double parentWidth = this.mainWrapper.getWidth();
		/*     */
		/* 587 */Double height = Double.valueOf(Math.min(parentHeight * 0.9D,
				this.imageGalleryHeight));
		/*     */
		/* 589 */this.factor = (height.doubleValue() / this.imageGalleryHeight);
		/* 590 */this.canvasWidth = ((int) (this.imageGalleryWidth * this.factor));
		/*     */
		/* 592 */if (this.canvasWidth > parentWidth) {
			/* 593 */this.factor = (parentWidth / this.imageGalleryWidth);
			/* 594 */this.canvasWidth = ((int) (this.imageGalleryWidth * this.factor));
			/* 595 */height = Double.valueOf(this.imageGalleryHeight
					* this.factor);
			/*     */}
		/*     */
		/* 598 */this.canvas.setWidth(this.canvasWidth);
		/* 599 */this.canvas.setHeight(height.doubleValue());
		/*     */}

	/*     */
	/*     */private final void syncParentStage(Stage parentStage)
	/*     */{
		/* 611 */setMinWidth(parentStage.getScene().getWidth());
		/* 612 */setMaxWidth(parentStage.getScene().getWidth());
		/* 613 */setMinHeight(parentStage.getScene().getHeight());
		/* 614 */setMaxHeight(parentStage.getScene().getHeight());
		/*     */
		/* 616 */this.mainWrapper
				.setMaxWidth(parentStage.getScene().getWidth());
		/* 617 */this.mainWrapper
				.setMinWidth(parentStage.getScene().getWidth());
		/*     */
		/* 619 */this.thumbnailsManager.setMaxWidth(parentStage.getScene()
				.getWidth());
		/* 620 */this.thumbnailsManager.setMinWidth(parentStage.getScene()
				.getWidth());
		/*     */
		/* 622 */doCalculations();
		/*     */}

	/*     */
	/*     */static final byte[] toByteArray(InputStream inputStream)
	/*     */throws IOException
	/*     */{
		/* 637 */ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		/*     */
		/* 639 */byte[] buffer = new byte[4096];
		/* 640 */int n = 0;
		/* 641 */while (-1 != (n = inputStream.read(buffer))) {
			/* 642 */outputStream.write(buffer, 0, n);
			/*     */}
		/*     */
		/* 645 */return outputStream.toByteArray();
		/*     */}
	/*     */
}

/*
 * Location: /media/store/Development/lab/imageGallery/ Qualified Name:
 * es.carlosmontero.javafx.gallery.Gallery JD-Core Version: 0.6.2
 */